package vip.bblog.cunadmin.modules.system.service.impl;


import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import vip.bblog.cunadmin.common.entity.PageResult;
import vip.bblog.cunadmin.modules.system.dto.ProjectParams;
import vip.bblog.cunadmin.modules.system.entity.Card;
import vip.bblog.cunadmin.modules.system.entity.Customer;
import vip.bblog.cunadmin.modules.system.entity.Publisher;
import vip.bblog.cunadmin.modules.system.mapper.CardMapper;
import vip.bblog.cunadmin.modules.system.mapper.PublisherMapper;
import vip.bblog.cunadmin.modules.system.service.CardService;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 单科考试
 * </p>
 * @author yy
 * @since
 */
@Service
public class CardServiceImpl extends ServiceImpl<CardMapper, Card> implements CardService {
    @Autowired
    private CardMapper cardMapper;
    @Autowired
    private PublisherMapper publisherMapper;

    @Override
    public PageResult<List<Card>> listPage(ProjectParams params) {
        Page<Card> page = params.getIPage();
        QueryWrapper<Card> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(params.getCardNum())) {
            queryWrapper.eq("card_num", params.getCardNum());
        }
        if (params.getIsActivation() != null) {
            queryWrapper.eq("is_activation", params.getIsActivation());
        }
        queryWrapper.orderByDesc("create_time");
        Page<Card> pageResult = this.page(page, queryWrapper);
        List<Card> list = pageResult.getRecords();
        for(Card c :list){
            c.setActivationText(c.getIsActivation() == 0 ? "否" : "是");
        }
        return PageResult.success(list, pageResult.getTotal());
    }

    @Override
    public int addCard(Card card) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String ctime = formatter.format(new Date());
        card.setCreateTime(ctime);
        cardMapper.insert(card);
        return 1;
    }


    @Override
    public void saveAll(MultipartFile excelFile) throws Exception {
        //参数1:文件输入流 参数2:实体类类型 参数3:导入的配置信息
        ImportParams params = new ImportParams();
        params.setTitleRows(0);  //文件标题所占行数
        params.setHeadRows(1);  //文件的列名所占的行数
        List<Card> list = ExcelImportUtil.importExcel(excelFile.getInputStream(), Card.class, params);
        for (Card eqt : list) {
            cardMapper.insert(eqt);
        }
    }

}
